package com.qdlc.p2p.dal.model;

import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.HttpUtil;
import com.qdlc.p2p.common.util.MD5Util;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.dto.AdditionalUserMovieTicket;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;

import java.util.Arrays;
import java.util.Date;

/**
 * Created by liaoxiangkai on 2015/11/28.
 */
public class AdditionalUserMovieTicketModel extends AdditionalUserMovieTicket {
    private static Logger logger = Logger.getLogger(AdditionalUserMovieTicketModel.class);

    public static final int USER_MOVIETICKE_UNUSED = 0; //未兑换
    public static final int USER_MOVIETICKE_USED = 1; //已兑换
    public static final int USER_MOVIETICKE_EXPIRE = 2; //过期
    public static final int USER_MOVIETICKE_INVALID = 3; //无效
    public static final int USER_MOVIETICKE_REQUEST = 4; //前往请求php接口/请求接口失败
    public static final int USER_MOVIETICKE_WAIT = 5; // 等待出票

    //用户名
    private String userName;
    //用户真实姓名
    private String realName;
    //规则名称
    private String ruleName;
    //搜索框输入字符串
    private String searchName;
    //兑换链接
    private String exchangeURL;
    //当前页
    private int page;
    //每页记录数
    private int rows;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getRuleName() {
        return ruleName;
    }

    public void setRuleName(String ruleName) {
        this.ruleName = ruleName;
    }

    public String getSearchName() {
        return searchName;
    }

    public void setSearchName(String searchName) {
        this.searchName = searchName;
    }

    public String getExchangeURL() {
        return exchangeURL;
    }

    public void setExchangeURL(String exchangeURL) {
        this.exchangeURL = exchangeURL;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    /**
     * 实例化
     * @param additionalUserMovieTicket
     * @return
     */
    public static AdditionalUserMovieTicketModel instance(AdditionalUserMovieTicket additionalUserMovieTicket) {
        AdditionalUserMovieTicketModel model = new AdditionalUserMovieTicketModel();
        BeanUtils.copyProperties(additionalUserMovieTicket, model);
        return model;
    }

    /**
     * 签名
     */
    public static String sign() {
        String channel_title = SystemConfigHelper.getValue(SystemConfigHelper.Nid.CHANNEL_TITLE);
        String channel_id = SystemConfigHelper.getValue(SystemConfigHelper.Nid.CHANNEL_ID);
        String create_time = DateUtil.dateStr2(new Date()); //格式yyyy-MM-dd
        String channel_key = SystemConfigHelper.getValue(SystemConfigHelper.Nid.CHANNEL_KEY);
        return MD5Util.MD5Encode(channel_title + channel_id + create_time + channel_key, "UTF-8");
    }

    /**
     * 请求php接口
     */
    public static String requestPHP(String url, NameValuePair[] data) {
        String sign = AdditionalUserMovieTicketModel.sign();
        NameValuePair[] constantData = {new NameValuePair("channel_id", SystemConfigHelper.getValue(SystemConfigHelper.Nid.CHANNEL_ID)), new NameValuePair("sign", sign)};
        NameValuePair[] temp = new NameValuePair[constantData.length + data.length];
        System.arraycopy(data, 0, temp, 0, data.length);
        System.arraycopy(constantData, 0, temp, data.length, constantData.length );
        logger.info(String.format("--->请求php：%s,请求参数：%s", url, Arrays.toString(temp)));
        String result = HttpUtil.doPost(url, temp);
        logger.info(String.format("--->请求php：%s,返回参数：%s", url, result));
        return result;
    }

}
